package com.appcenter.application.controller;

import com.appcenter.application.input.permissiongroup.*;
import com.appcenter.application.input.permissiongrouprelation.CreatePermissionGroupRelationInput;
import com.appcenter.application.service.PermissionGroupService;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/jzappcenter/permissiongroup")
@Api("权限组API")
public class PermissionGroupMapper {
    @Autowired
    PermissionGroupService permissionGroupService;

    @ApiOperation(value = "加载权限组", notes = "加载权限组",produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> PageLoad(@RequestBody PageLoadInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setSuccess(true);
            r.setResult(permissionGroupService.PageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "查询权限组", notes = "查询权限组",produces = "application/json")
    @PostMapping("/getpermissiongroups")
    public ResponseEntity<JsonResult> getPermissionGroups(@RequestBody GetPermissionGroupsInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setSuccess(true);
            r.setResult(permissionGroupService.GetPermissionGroups(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "创建权限组", notes = "创建权限组",produces = "application/json")
    @PostMapping("/createpermissiongroup")
    public ResponseEntity<JsonResult>createPermissionGroup(@RequestBody CreatePermissionGroupInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setSuccess(true);
            r.setResult(permissionGroupService.CreatePermissionGroup(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "编辑权限组", notes = "编辑权限组",produces = "application/json")
    @PostMapping("/editpermissiongroup")
    public ResponseEntity<JsonResult> editPermissionGroup(@RequestBody EditPermissionGroupInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setSuccess(true);
            r.setResult(permissionGroupService.EditPermissionGroup(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "删除权限组", notes = "删除权限组",produces = "application/json")
    @PostMapping("/deletepermissiongroup")
    public ResponseEntity<JsonResult>deletePermissionGroup(@RequestBody DeletePermissionGroupInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setSuccess(true);
            r.setResult(permissionGroupService.DeletePermissionGroup(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "保存权限组权限", notes = "保存权限组权限",produces = "application/json")
    @PostMapping("/createpermissiongrouprelation")
    public ResponseEntity<JsonResult>createPermissionGroupRelation(@RequestBody CreatePermissionGroupRelationInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setSuccess(true);
            r.setResult(permissionGroupService.CreatePermissionGroupRelation(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }
}
